// 慎用全局变量
// 全局变量定义在全局执行上下文，是所有作用域链的顶端
// 全局执行上下文一直存在于上下文执行栈，直到程序退出
// 如果某个局部作用域出现了同名变量，则会屏蔽或污染全局变量

function fn() {
    const name = "张三"
    return function () {
        console.log(name)
    }
}
const a = fn()
a()


function Person() {
    this.name = "erewre"
    this.age = 18
    this.getAge = function() {
        return this.age
    }
}
const p1 = new Person()
const a = p1.getAge()

function Person() {
    this.name = "erewre"
    this.age = 18
    
}
const p2 = new Person()
const b = p2.age

// 循环优化
const ps = document.getElementsByClassName("ps")

for (let i = 0; i < ps.length; i++) {
    console.log(i)
}

for (let i = 0, len = ps.length; i <len; i++) {
    console.log(i)
}


// 添加节点优化
for (let i = 0; i < 10; i++) {
    var o = document.createElement("p")
    o.innerHTML = i
    document.body.appendChild(o)
}
const fraget = document.createDocumentFragment()
for (let i = 0; i < 10; i++) {
    var o = document.createElement("p")
    o.innerHTML = i
    fraget.appendChild(o)
}
document.body.appendChild(fraget)

//  
for (let i = 0; i < 3; i++) {
    var o = document.createElement("p")
    o.innerHTML = i
    document.body.appendChild(o)
}

var oldP = document.getElementById("box1")
for (let i = 0; i < 3; i++) {
    var newP = oldP.cloneNode(false)
    newP.innerHTML = i
    document.body.appendChild(newP)
}
// 直接量替换 new Object
var a = [1,2,3]
var b = new Array(3)
b[0] = 1
b[0] = 2
b[0] = 3

